package com.dsc.shoppcar.servlet;

import com.dsc.shoppcar.dao.DataMoniDao;
import com.dsc.shoppcar.pojo.Goods;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Du Shun Chang
 * @title: LookShoppingCartServlet
 * @date: 2022/4/14 8:36
 * @description: TODO
 * @version：1.0
 */
public class LookShoppingCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Goods> list = new ArrayList<Goods>();
        list = DataMoniDao.getData();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        Cookie[] cookies = request.getCookies();
        StringBuffer sf = new StringBuffer();
        sf.append("<html>");
        sf.append("<head>");
        sf.append("<title>购物车页面</title>");
        sf.append("</head>");
        sf.append("<body>");
        sf.append("<h2>这里是购物车页面</h2>");
        sf.append("<table border='1'>");
        sf.append("<tr>");
        sf.append("<td>商品名</td><td>图片</td><td>数量</td><td>总价格</td><td>操作</td>");
        sf.append("</tr>");
        // 遍历cookie中存储的商品信息，依次展示到购物车页面中
        if(cookies != null) {
            for(Cookie cookie : cookies ){
                // 获取cookie名，进行判断，确定是购物车应用的cookie信息 ， 此处省略...
                String cookie_name = cookie.getName();
                String cookie_val = cookie.getValue();
                String id = null;
                if(!"".equals(cookie_val)){
//                    id = String.(cookie_val);
//                    id = Integer.parseInt(cookie_val);
                }
                // 取出商品信息
                Goods goods = null ;
                for(Goods gs : list) {
                    if(gs.getId() != null) {
                        goods = gs ;
                        break;
                    }
                }
                if(goods != null) {
                    sf.append("<tr>");
                    sf.append("<td>" + goods.getName() + "</td>");
                    sf.append("<td><img src=\"" + goods.getImg_url() + "\" height=\"60px\"></td>");
                    // 数量暂时写死，将太多数据存入cookie不安全
                    sf.append("<td>1</td>");
                    // 总价要根据数量计算，此处只取单价
                    sf.append("<td>" + goods.getPrice() + "</td>");
                    // 结算按钮暂不制作，有兴趣的同学可以继续制作
                    sf.append("<td><a href=\"javascript:void(0)\">结算</a></td>");
                    sf.append("</tr>");
                }
            }
        } else {
            sf.append("<tr>");
            sf.append("<td colspan = '5'><span style='color:red'>您还没有选择商品！</span></td>");
            sf.append("</tr>");
        }

        sf.append("</table>");
        sf.append("</body>");
        sf.append("</html>");

        out.write(sf.toString());
        out.close();
    }



}
